<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="search-form">
        <el-form-item label="文章标题" prop="healthyTitle">
          <el-input
            v-model="queryParams.healthyTitle"
            placeholder="请输入文章标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="作者" prop="healthyProple">
          <el-input
            v-model="queryParams.healthyProple"
            placeholder="请输入作者"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="文章内容" prop="healthyBody">
          <el-input
            v-model="queryParams.healthyBody"
            placeholder="请输入文章内容"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="上传时间" prop="createDate">
          <el-date-picker clearable
            v-model="queryParams.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:healthy:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:healthy:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:healthy:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:healthy:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="healthyList"
        @selection-change="handleSelectionChange"
        :row-class-name="tableRowClassName"
        class="healthy-table">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="文章id" align="center" prop="healthyId" />
        <el-table-column label="文章标题" align="center" prop="healthyTitle" />
        <el-table-column label="文章照片" align="center" prop="healthyImage" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.healthyImage" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="作者" align="center" prop="healthyProple" />
        <el-table-column label="文章内容" align="center" prop="healthyBody">
          <template slot-scope="scope">
            {{ scope.row.healthyBody && scope.row.healthyBody.length > 30
              ? scope.row.healthyBody.slice(0, 30) + '……'
              : scope.row.healthyBody }}
          </template>
        </el-table-column>
        <el-table-column label="文章状态" align="center" prop="healthyStatus" >
          <template slot-scope="scope">
            <span v-if="scope.row.healthyStatus == 1">
              <el-button type="success" icon="el-icon-check" circle></el-button>
            </span>
            <span v-else>
              <el-button type="danger" icon="el-icon-close" circle></el-button>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="上传时间" align="center" prop="createDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:healthy:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:healthy:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      class="healthy-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="healthyTitle">
          <el-input v-model="form.healthyTitle" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章照片" prop="healthyImage">
          <image-upload v-model="form.healthyImage"/>
        </el-form-item>
        <el-form-item label="作者" prop="healthyProple">
          <el-input v-model="form.healthyProple" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="文章内容" prop="healthyBody">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入文章内容"
            v-model="form.healthyBody">
          </el-input>
        </el-form-item>
        <el-form-item label="上传时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHealthy, getHealthy, delHealthy, addHealthy, updateHealthy } from "@/api/system/healthy"

export default {
  name: "Healthy",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      healthyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        healthyTitle: null,
        healthyImage: null,
        healthyProple: null,
        healthyBody: null,
        healthyStatus: null,
        createDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listHealthy(this.queryParams).then(response => {
        this.healthyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        healthyId: null,
        healthyTitle: null,
        healthyImage: null,
        healthyProple: null,
        healthyBody: null,
        healthyStatus: null,
        createDate: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.healthyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加【请填写功能名称】"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const healthyId = row.healthyId || this.ids
      getHealthy(healthyId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改【请填写功能名称】"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.healthyId != null) {
            updateHealthy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addHealthy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const healthyIds = row.healthyId || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + healthyIds + '"的数据项？').then(function() {
        return delHealthy(healthyIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/healthy/export', {
        ...this.queryParams
      }, `healthy_${new Date().getTime()}.xlsx`)
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 === 0) {
        return 'even-row'
      } else {
        return 'odd-row'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);

  .box-card {
    border-radius: 12px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.1);
      transform: translateY(-2px);
    }
  }

  .search-form {
    margin-bottom: 24px;
    padding: 24px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

    .el-form-item {
      margin-bottom: 0;
      margin-right: 16px;

      &:last-child {
        margin-right: 0;
      }
    }

    .el-input {
      .el-input__inner {
        border-radius: 4px;
        &:focus {
          border-color: #409EFF;
          box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }
      }
    }

    .el-button {
      padding: 9px 20px;
      border-radius: 4px;
      font-weight: 500;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }
    }
  }

  .mb8 {
    margin-bottom: 16px;
    padding: 0 8px;

    .el-button {
      padding: 8px 16px;
      border-radius: 4px;
      font-weight: 500;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }

      i {
        margin-right: 4px;
      }
    }
  }

  .healthy-table {
    margin-top: 16px;
    border-radius: 8px;
    overflow: hidden;

    .el-table {
      border-radius: 8px;
      overflow: hidden;

      &::before {
        display: none;
      }

      th {
        background-color: #f5f7fa !important;
        color: #606266;
        font-weight: 600;
        height: 50px;
      }

      td {
        padding: 12px 0;
      }
    }

    .even-row {
      background-color: #fafafa;
    }

    .odd-row {
      background-color: #fff;
    }

    .el-table__row {
      transition: all 0.3s;

      &:hover {
        background-color: #f5f7fa !important;
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
      }
    }

    .el-button--text {
      padding: 4px 8px;
      font-size: 13px;

      &:hover {
        color: #409EFF;
        background-color: rgba(64, 158, 255, 0.1);
        border-radius: 4px;
      }
    }
  }

  .healthy-dialog {
    .el-dialog {
      border-radius: 12px;
      overflow: hidden;
      box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);

      .el-dialog__header {
        padding: 20px 24px;
        border-bottom: 1px solid #ebeef5;
        background-color: #f5f7fa;

        .el-dialog__title {
          font-size: 18px;
          font-weight: 600;
          color: #303133;
        }
      }

      .el-dialog__body {
        padding: 24px;
      }

      .el-dialog__footer {
        padding: 16px 24px;
        border-top: 1px solid #ebeef5;
        background-color: #f5f7fa;
      }
    }

    .el-form-item {
      margin-bottom: 24px;

      &:last-child {
        margin-bottom: 0;
      }

      .el-form-item__label {
        font-weight: 500;
        color: #606266;
      }
    }

    .el-input__inner {
      border-radius: 4px;

      &:focus {
        border-color: #409EFF;
        box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
      }
    }

    .dialog-footer {
      .el-button {
        padding: 9px 20px;
        border-radius: 4px;
        font-weight: 500;
        transition: all 0.3s;

        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }
      }
    }
  }

  .pagination-container {
    padding: 16px 0;
    text-align: right;
  }

  .image-preview {
    border-radius: 6px;
    overflow: hidden;
    transition: all 0.3s;
    cursor: pointer;

    &:hover {
      transform: scale(1.05);
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    }
  }
}
</style>
